package com.wsy.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.wsy.entity.Bookinfo;
import com.wsy.entity.Emp;
import com.wsy.entity.ResponseMsg;
import com.wsy.service.BookInfoService;
import com.wsy.util.FileUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("bookinfo")
public class BookInfoController {
    @Resource
    BookInfoService bookInfoService;

    @RequestMapping("select")
    public PageInfo<Bookinfo> select(Integer pageNum, Integer pageSize) {
        return bookInfoService.select(pageNum, pageSize);
    }

    @RequestMapping("insert")
    public ResponseMsg addBook(Bookinfo bookinfo) {
        return new ResponseMsg(200,"添加成功",bookInfoService.addBook(bookinfo));
    }

    @RequestMapping("addImg")
    public Map<Object, String> addImgPost(MultipartFile file, Bookinfo bookinfo){
        Map<Object, String> map = new HashMap();
        String upload = FileUtil.uploadFront(file);
        bookinfo.setPic(upload);
        String imgpath=upload.substring(upload.lastIndexOf("src"));
        map.put("pic",imgpath);
        return map;
    }

    @RequestMapping("update")
    public ResponseMsg updBook(Bookinfo bookinfo) {
        return new ResponseMsg(200,"修改成功",bookInfoService.updBook(bookinfo));
    }

    @RequestMapping("delete")
    public ResponseMsg delete(String id,Integer state){
        UpdateWrapper<Bookinfo> wrapper=new UpdateWrapper<>();
        wrapper.set("state",state);
        wrapper.eq("id",id);
        String msg = null;
        if(state==0){
            msg="已下架";
        }else if (state==1){
            msg="已上架";
        }
        return  new ResponseMsg(200,msg,bookInfoService.update(wrapper));
    }
}
